<script setup>
import { useRoute, useRouter } from 'vue-router'
import { useStore } from '../store/store'

// 获取store实例
const store = useStore()
// 当前路由对象
const curRoute = useRoute()
// 获取路由实例
const router = useRouter()

// 获取用户信息
const userInfo = JSON.parse(localStorage.getItem('userInfo'))
// 切换侧边栏
function toggle() {
  store.changeAsideIshow()
}
// 关闭菜单
function closeTab(item, index) {
  // 如果是选中的菜单
  if (curRoute.fullPath === item.path) {
    // 删除当前菜单
    store.delMenu(item)
    if (store.selectMenu.length === 0) {
      // 跳转首页
      router.push('/')
    } else if (store.selectMenu[index]) {
      // 跳转后一个菜单
      router.push(store.selectMenu[index].path)
    } else {
      // 跳转前一个菜单
      router.push(store.selectMenu[index - 1].path)
    }
  } else {
    // 非选中菜单
    // 删除菜单
    store.delMenu(item)
  }
}

// 跳转菜单
function skip(item, index) {
  // 跳转指定路由
  router.push(item.path)
  // 修改侧边栏选中状态
  store.updateMenuActive(item.active)
}

// 退出按钮
function handleClick(command) {
  if (command === 'cancel') {
    // 退出登录
    // 清除所有缓存
    store.clearUserCache()
    // 触发路由守卫 跳转登录页
    window.location.href = window.location.origin
  }
}
</script>

<template>
  <div class="header-container">
    <div class="header-left flex-box">
      <el-icon class="icon" size="20" @click="toggle"><Fold /></el-icon>
      <ul class="menu-list flex-box">
        <li
          v-for="(item, index) in store.selectMenu"
          :key="`${index}-${item.id}`"
          class="tab flex-box"
          :class="{ selected: item.path === curRoute.fullPath }"
        >
          <div @click="skip(item, index)">
            <el-icon size="12">
              <component :is="item.icon"></component>
            </el-icon>
            <span>
              {{ item.name }}
            </span>
          </div>

          <el-icon class="closeBtn" size="14" @click="closeTab(item, index)"
            ><Close
          /></el-icon>
        </li>
      </ul>
    </div>
    <div class="header-right">
      <el-dropdown @command="handleClick">
        <span class="el-dropdown-link flex-box">
          <el-avatar :src="userInfo.avatar"></el-avatar>
          <p class="username">{{ userInfo.name }}</p>
        </span>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item command="cancel">退出</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
  </div>
</template>

<style scoped lang="less">
a {
  height: 100%;
  color: #333;
  font-size: 15px;
  display: flex;
  align-items: center;
  padding: 0 6px;
}

.flex-box {
  display: flex;
  align-items: center;
}

.header-container {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 100%;
  background-color: #fff;
  padding-right: 25px;
  .header-left {
    height: 100%;
    .icon {
      width: 45px;
      height: 100%;
    }
    .icon:hover {
      background-color: #f5f5f5;
      cursor: pointer;
    }
    .menu-list {
      height: 100%;
      .tab {
        align-items: center;
        padding: 0 10px;
        height: 100%;
        cursor: pointer;
        span {
          padding: 0 6px;
        }
        .closeBtn {
          opacity: 0;
        }
      }
      .tab:hover {
        background-color: #f5f5f5;
        .closeBtn {
          opacity: 1;
          cursor: pointer;
        }
      }
      .selected {
        color: #409eff;
        a {
          color: #409eff;
        }
        background-color: #f5f5f5;
        .closeBtn {
          color: #000;
        }
      }
    }
  }
  .header-right {
    .username {
      padding-left: 5px;
    }
  }
}
</style>
